<template>
  <div>
    <!-- <el-form ref="form" :rules="rules" :model="form" label-width="80px"> -->
    <el-form ref="formRef" :rules="rules" :model="form" label-width="80px">

      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>

      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>

      <el-form-item label="部门负责人">
        <el-select v-model="form.manager" placeholder="请选择活动区域" prop="manager">
          <!-- <el-option label="区域一" value="shanghai" /> -->
          <el-option v-for="item in list" :key="item.id" :label="item.username " :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{ isEdit?'编辑':'新增' }}</el-button>
        <el-button @click="remveShan">取消</el-button>
      </el-form-item>
    </el-form></div>
</template>

<script>
import { UpemPloyees } from '@/api/emPloyees'
import { addDepartment } from '@/api/addBumen'

import { bianjiXiu } from '@/api/editXiugai'
import { updataXiu } from '@/api/editXiugai'

// console.log('接口', UpemPloyees())
export default {
  props: {
    currentPid: {
      type: String,
      default: ''
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    arrlistTochild: {
      type: Array,
      default: () => []
    }
  },
  data() {
    // 因为每一个函数再掉能用的时候,就会动态创建一个this,所以就会说找不到this,就会要定义个this用变量接受
    const that = this
    const validateCode = function(rule, value, callback) {
      console.log('代码执行没有', value)
      let codeList = that.arrlistTochild.map(item => item.code)
      if (that.isEdit) {
        codeList = that.arrlistTochild.filter(item => item.id !== that.currentPid).map(item => item.code)
      }
      // console.log(codeList)
      if (!value) {
        return callback('code需要填写')
      }
      if (codeList.includes(value)) {
        callback(new Error(`当前value:${value} 值重复`))
      } else {
        callback()
      }
      console.log(codeList)
      // 必须的
      callback()
    }
    const validateName = (rule, value, callback) => {
      let nameList = this.arrlistTochild.filter(item => item.pid === this.currentPid).map(item => item.name)
      if (this.isEdit) {
        const editParent = this.arrlistTochild.find(item => item.id === this.currentPid)
        const xiongdi = this.arrlistTochild.filter(item => item.pid === editParent.pid)
        nameList = xiongdi.filter(item => item.id !== this.currentPid).map(item => item.name)
      }
      if (nameList.includes(value)) {
        callback(new Error(`当前部门名称:${value}重复`))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      list: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          // { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          // { required: true, message: '部门编码', trigger: 'blur' },
          // { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { type: 'date', required: true, message: '请选择负责人', trigger: 'change' }

        ],
        introduce: [
          { type: 'date', required: true, message: '请部门介绍', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100个字符', trigger: 'blur' }

        ]
      }
    }
  },
  created() {
    console.log('开始', this.arrlistTochild)
    // 接口方法要调用
    this.UpemPloyeesWei()
    // console.log('维维', addDepartment())
    this.bianJI()
  },
  destroyed() {
    console.log('这个组件是否销毁了')
  },
  methods: {
    // 打开编辑修改数据
    async bianJI() {
      if (this.isEdit) {
        const res = await bianjiXiu(this.currentPid)
        console.log(res)
        this.form = res?.data
      }
    },
    // 修改完的
    async weiWei() {
      const res = await updataXiu(this.form)
      if (res.success) {
        this.$emit('chenggong')
      } else {
        this.$message.error(res.message)
      }
    },
    onSubmit() {
      // this.$refs.formRef.validate 兜底校验如果val为真的 才会走确认渲染编辑或者是新增,为真的意思就是兜底校验成功有一个人没有写就不行
      this.$refs.formRef.validate(val => {
        if (val) {
          if (this.isEdit) {
            this.weiWei()
          } else {
            this.oneWei()
          }
        }
      })
    },
    remveShan() {
      this.$emit('ShanChu')
      // this.oneWei()
    },
    async  UpemPloyeesWei() {
      try {
        const res = await UpemPloyees()
        this.list = res.data
        console.log('成功获取', res)
      } catch (err) {
        console.log('请求错误', err)
      }
    },
    async oneWei() {
      // 新增的数据
      try {
        const res = await addDepartment({ ...this.form, pid: this.currentPid })
        console.log('请求成功过', res)
        if (res.success) {
          this.$emit('chenggong')
        } else {
          this.$message.error(res.message)
        }
      } catch (err) {
        console.log(err)
      }
    }

  }
}
</script>

<style>

</style>
